﻿Imports TransportadoraToledoDAL
Imports System.Data

Public Class tiposCargasBLL
    Dim objDAL As tiposCargasDAL

#Region "propriedades"

    Private codigo As Integer
    Public Property pCodigo() As Integer
        Get
            Return codigo
        End Get
        Set(ByVal value As Integer)
            codigo = value
        End Set
    End Property

    Private descricao As String
    Public Property pDescricao() As String
        Get
            Return descricao
        End Get
        Set(ByVal value As String)
            descricao = value
        End Set
    End Property

    Private status As Char
    Public Property pStatus() As Char
        Get
            Return status
        End Get
        Set(ByVal value As Char)
            status = value
        End Set
    End Property

    Private Verifica As Boolean
    Public Property pVerifica() As Boolean
        Get
            Return Verifica
        End Get
        Set(ByVal value As Boolean)
            Verifica = value
        End Set
    End Property


#End Region

#Region "Métodos"
    Sub New()

    End Sub

    Sub New(ByVal codigo As Integer, ByVal desc As String, ByVal status As Char)
        Me.codigo = codigo
        Me.descricao = desc
        Me.status = status

    End Sub

    Sub inserir()
        Try
            If (descricao = "" Or status = "") Then
                Throw New Exception("Preencha todos os campos!")
            Else
                'inserir
                objDAL = New tiposCargasDAL
                Me.codigo = objDAL.inserir(descricao, status)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub alterar()
        Try
            If (codigo = 0 Or descricao = "" Or status = "") Then
                Throw New Exception("Preencha todos os campos!")
            Else
                'alterar
                objDAL = New tiposCargasDAL
                objDAL.alterar(codigo, descricao, status)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub excluir()
        Try
            If (codigo = 0) Then
                Throw New Exception("Campo(s) inválido(s)!")
            Else
                'excluir
                objDAL = New tiposCargasDAL
                objDAL.excluir(codigo)
            End If
        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    'retorna todas as linhas pesquisadas da tabela
    Function localizar(ByVal descricao As String) As DataTable
        Dim tab As DataTable = Nothing
        Try

            objDAL = New tiposCargasDAL
            tab = objDAL.localizar(descricao)

        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return tab
    End Function


    'retorna todas as linhas pesquisadas da tabela
    Function localizarPrimeiroUltimo(ByVal descricao As String) As Integer
        Dim tab As DataTable = Nothing
        Dim codigo As Integer = 0
        Try
            If (descricao = "ultimo") Then
                objDAL = New tiposCargasDAL
                tab = objDAL.localizar(descricao)
                If (tab.Rows(0)("tc_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("tc_codigo").ToString
                End If
            ElseIf (descricao = "primeiro") Then
                objDAL = New tiposCargasDAL
                tab = objDAL.localizar(descricao)
                If (tab.Rows(0)("tc_codigo").ToString <> "") Then
                    codigo = tab.Rows(0)("tc_codigo").ToString
                End If
            End If


        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return codigo
    End Function

    Function localizarProxAnteriro(ByVal descricao As String, ByVal codigo As Integer) As Integer
        Dim tab As DataTable = Nothing
        Dim CodigoP As Integer = 0
        Try

            objDAL = New tiposCargasDAL
            tab = objDAL.localizarProxAnterior(descricao, codigo)
            If (tab.Rows.Count > 0) Then
                CodigoP = tab.Rows(0)("tc_codigo").ToString
            End If


        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
        Return CodigoP
    End Function

    'localizar uma linha da tabela w preenche as propriedades
    Sub localizar(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            objDAL = New tiposCargasDAL
            tab = objDAL.localizar(codigo)
            If (tab.Rows.Count > 0) Then
                Me.codigo = tab.Rows(0)("tc_codigo").ToString
                Me.descricao = tab.Rows(0)("tc_descricao").ToString
                Me.status = tab.Rows(0)("tc_ativo_sn").ToString
            Else
                limpar()
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Sub limpar()
        Me.codigo = Nothing
        Me.descricao = Nothing
        Me.status = Nothing
    End Sub

    Sub VerificaExclusao(ByVal codigo As Integer)
        Try

            objDAL = New tiposCargasDAL
            Me.Verifica = objDAL.VerificaExclusao(codigo)

        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

    Sub VerificaSituacao(ByVal codigo As Integer)
        Try

            objDAL = New tiposCargasDAL
            Me.Verifica = objDAL.VerificaSituacao(codigo)

        Catch ex As Exception
            Throw ex
        End Try
        objDAL = Nothing
    End Sub

#End Region
End Class